/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.smg.community.service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Service;

import javacommon.base.*;

import cn.org.rapid_framework.page.*;

import com.smg.community.dao.CrmRoleDao;
import com.smg.community.model.CrmRole;
import com.smg.community.vo.query.CrmRoleQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Service
public class CrmRoleManager extends BaseManager<CrmRole,java.lang.Integer>{

	private CrmRoleDao crmRoleDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCrmRoleDao(CrmRoleDao dao) {
		this.crmRoleDao = dao;
	}
	
	private static ConcurrentHashMap<String,CrmRole> RoleCacheMap = new ConcurrentHashMap<String,CrmRole>(10);
	
	
	public EntityDao getEntityDao() {
		return this.crmRoleDao;
	}
	/**
	 * 获取所有权限
	 * */
	public List<CrmRole> getAll(){
		return crmRoleDao.getAll();
	}
	
	/**
	 * 根据角色id 获取相应角色
	 * */
	public List<CrmRole> getByIds(String ids){
		return crmRoleDao.getByIds(ids);
	}
	
	public Page findPage(CrmRoleQuery query) {
		return crmRoleDao.findPage(query);
	}
	public CrmRole getRoleByRoleQuery(CrmRoleQuery query) {
		return crmRoleDao.getRoleByRoleQuery(query);
	}
	public List<CrmRole> getBy(CrmRoleQuery query) {
		return crmRoleDao.getBy(query);
	}
	/**
	 * 根据传进来的roleids 和 角色名 判断该用户是否拥有该角色
	 * */
	public boolean isRole(String roleids,String roleName){
		CrmRole bdManagerRole = RoleCacheMap.get(roleName);
		if (bdManagerRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName(roleName);
			bdManagerRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put(roleName, bdManagerRole);
		}
		int bdManagerRoleId = bdManagerRole.getId();
		String bdManagerRoleIdStr = String.valueOf(bdManagerRoleId);
		if(roleids.indexOf(","+bdManagerRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	/**
	 * 根据传进来的roleids判断该用户是否VP主管角色
	 * @param roleids
	 * @return
	 */
	public boolean isVp(String roleids) {
		CrmRole vpRole = RoleCacheMap.get("VP");
		if( vpRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("VP");
			vpRole =getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("VP", vpRole);
		}
		int vpRoleId = vpRole.getId();
		String vpRoleIdStr = String.valueOf(vpRoleId);
		if(roleids.indexOf(","+vpRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	
	/**
	 * 根据传进来的roleids判断该用户是否有GM角色
	 * @param roleids
	 * @return
	 */
	public boolean isGm(String roleids) {
		CrmRole gmRole =RoleCacheMap.get("分站GM");
		if( gmRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("分站GM");
			gmRole =getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("分站GM", gmRole);
		}
		int gmRoleId = gmRole.getId();
		String gmRoleIdStr = String.valueOf(gmRoleId);
		if(roleids.indexOf(","+gmRoleIdStr+",") !=-1){
			return true;
		}
		gmRole = RoleCacheMap.get("总部GM");
		if( gmRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("总部GM");
			gmRole =getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("总部GM", gmRole);
		}
	    if(gmRole!=null){
	    	gmRoleId = gmRole.getId();
	    	gmRoleIdStr = String.valueOf(gmRoleId);
			if(roleids.indexOf(","+gmRoleIdStr+",") !=-1){
				return true;
			}
	    }
		return false;
	}
	
	public boolean isBdAssistant(String roleids){
		CrmRole assistantRole =RoleCacheMap.get("BD助理");
		if( assistantRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("BD助理");
			assistantRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("BD助理", assistantRole);
		}
		int assistantRoleId = assistantRole.getId();
		String assistantRoleIdStr = String.valueOf(assistantRoleId);
		if(roleids.indexOf(","+assistantRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	
	/**
	 * 根据传进来的roleids判断该用户是否有BD经理角色
	 * @param roleids
	 * @return
	 */
	public boolean isBdManager(String roleids){
		CrmRole bdManagerRole = RoleCacheMap.get("BD经理");
		if (bdManagerRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("BD经理");
			bdManagerRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("BD经理", bdManagerRole);
		}
		int bdManagerRoleId = bdManagerRole.getId();
		String bdManagerRoleIdStr = String.valueOf(bdManagerRoleId);
		if(roleids.indexOf(","+bdManagerRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	/**
	 * 根据传进来的roleids判断该用户是否有BD主管角色
	 * @param roleids
	 * @return
	 */
	public boolean isBdMaster(String roleids) {
		CrmRole bdMasterRole = RoleCacheMap.get("BD主管");
		if (bdMasterRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("BD主管");
			bdMasterRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("BD主管", bdMasterRole);
		}
		int bdMasterRoleId = bdMasterRole.getId();
		String bdMasterRoleIdStr = String.valueOf(bdMasterRoleId);
		if(roleids.indexOf(","+bdMasterRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	/**
	 * 根据传进来的roleids判断该用户是否有BD角色
	 * @param roleids
	 * @return
	 */
	public boolean isBd(String roleids) {
		CrmRole bdRole =  RoleCacheMap.get("BD");
		if ( bdRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("BD");
			bdRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("BD", bdRole);
			
		}
		int bdRoleId = bdRole.getId();
		String bdRoleIdStr = String.valueOf(bdRoleId);
		if(roleids.indexOf(","+bdRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	/**
	 * 根据传进来的roleids判断该用户是否有运营角色
	 * @param roleids
	 * @return
	 */
	public boolean isYunYing(String roleids) {
	
		CrmRole yunyingRole =   RoleCacheMap.get("运营");
		if( yunyingRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("运营");
			yunyingRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("运营",yunyingRole);
		}
		int yunyingRoleId = yunyingRole.getId();
		String yunyingRoleIdStr = String.valueOf(yunyingRoleId);
		if(roleids.indexOf(","+yunyingRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 根据传进来的roleids判断该用户是否有门店审核角色
	 * @param roleids
	 * @return
	 */
	public boolean isPoiShenhe(String roleids){
		CrmRole poiShenhe =   RoleCacheMap.get("门店审核");
		if( poiShenhe == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("门店审核");
			poiShenhe = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("门店审核",poiShenhe);
		}
		int yunyingRoleId = poiShenhe.getId();
		String yunyingRoleIdStr = String.valueOf(yunyingRoleId);
		if(roleids.indexOf(","+yunyingRoleIdStr+",") !=-1){
			return true;
		}
		return false;
		
	}
	//是否是文档管理员权限
	public boolean isDocManager(String value) {
		CrmRole poiShenhe =   RoleCacheMap.get("文档管理员");
		if( poiShenhe == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("文档管理员");
			poiShenhe = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("文档管理员",poiShenhe);
		}
		int DocManageRoleId = poiShenhe.getId();
		String DocManagegRoleIdStr = String.valueOf(DocManageRoleId);
		if(value.indexOf(","+DocManagegRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	/**
	 * 根据传进来的roleids判断该用户是否有菜品审核角色
	 * @param roleids
	 * @return
	 */
	public boolean isCourseAudit(String roleids){
		CrmRole courseShenheRole =   RoleCacheMap.get("菜品审核");
		if( courseShenheRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("菜品审核");
			courseShenheRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("菜品审核",courseShenheRole);
		}
		int courseShenheRoleId = courseShenheRole.getId();
		String courseShenheRoleIdStr = String.valueOf(courseShenheRoleId);
		if(roleids.indexOf(","+courseShenheRoleIdStr+",") !=-1){
			return true;
		}
		return false;
		
	}
	/**
	 * 根据传进来的roleids判断该用户是否有总部合同审核角色
	 * @param roleids
	 * @return
	 */
	public boolean isZongbu(String roleids){
		CrmRole courseShenheRole =   RoleCacheMap.get("总部合同管理员");
		if( courseShenheRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("总部合同管理员");
			courseShenheRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("总部合同管理员",courseShenheRole);
		}
		int courseShenheRoleId = courseShenheRole.getId();
		String courseShenheRoleIdStr = String.valueOf(courseShenheRoleId);
		if(roleids.indexOf(","+courseShenheRoleIdStr+",") !=-1){
			return true;
		}
		return false;
		
	}
	
	
	/**
	 * 根据传进来的roleids判断该用户是否有分站合同审核角色
	 * @param roleids
	 * @return
	 */
	public boolean isFenzhan(String roleids){
		CrmRole courseShenheRole =   RoleCacheMap.get("分站合同管理员");
		if( courseShenheRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("分站合同管理员");
			courseShenheRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("分站合同管理员",courseShenheRole);
		}
		int courseShenheRoleId = courseShenheRole.getId();
		String courseShenheRoleIdStr = String.valueOf(courseShenheRoleId);
		if(roleids.indexOf(","+courseShenheRoleIdStr+",") !=-1){
			return true;
		}
		return false;
		
	}
	public boolean isConfirmContractNumber(String roleIds) {
		CrmRole courseShenheRole =   RoleCacheMap.get("合同编号确认员");
		if( courseShenheRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("合同编号确认员");
			courseShenheRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("合同编号确认员",courseShenheRole);
		}
		int courseShenheRoleId = courseShenheRole.getId();
		String courseShenheRoleIdStr = String.valueOf(courseShenheRoleId);
		if(roleIds.indexOf(","+courseShenheRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
	public boolean isContractEditor(String roleIds) {
		CrmRole courseShenheRole =   RoleCacheMap.get("合同编辑员");
		if( courseShenheRole == null){
			CrmRoleQuery  crmRoleQuery = new CrmRoleQuery();
			crmRoleQuery.setName("合同编辑员");
			courseShenheRole = getRoleByRoleQuery(crmRoleQuery);
			RoleCacheMap.put("合同编辑员",courseShenheRole);
		}
		int courseShenheRoleId = courseShenheRole.getId();
		String courseShenheRoleIdStr = String.valueOf(courseShenheRoleId);
		if(roleIds.indexOf(","+courseShenheRoleIdStr+",") !=-1){
			return true;
		}
		return false;
	}
}
